﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;

namespace HashtableTest
{
    class Program
    {
         

        static void Main(string[] args)
        {
            Hashtable ht = new Hashtable();  
            ht.Add("北京", "帝都");  
            ht.Add("上海", "魔都");
            ht.Add("广州", "省会");
            ht.Add("深圳", "特区");
            string capital = (string)ht["北京"];
            Console.WriteLine(ht.Contains("上海")); //判断哈希表是否包含特定键,其返回值为true或false
            ht.Remove("深圳"); //移除一个keyvalue键值对
            ht.Clear(); //移除所有




             




            Hashtable hashtable = GetHashtable();
            string name = (string)hashtable["名字"];
            Console.WriteLine(name);
            int age = (int)hashtable["年龄"];
            Console.WriteLine(age);







            hashtable.Add(100, "西安");
             
            string value = hashtable[100] as string;
            if (value != null)
            {
                Console.WriteLine(value);
            }

            if (!(hashtable[100] is StreamReader reader))
            {
                Console.WriteLine("西安不是StreamReader型");
            }
             
            object value2 = hashtable[100];
            if (value2 is string)
            {
                Console.Write("这个是字符串型: ");
                Console.WriteLine(value2);
            }





            int count = 1000000;
            Dictionary<int, int> dictionary = new Dictionary<int, int>();
            Hashtable hashtable1 = new Hashtable();
            for (int i = 0; i < count; i++)
            {
                dictionary.Add(i, i);
                hashtable1.Add(i, i);
            }

            Stopwatch stopwatch = Stopwatch.StartNew();
            for (int i = 0; i < count; i++)
            {
                int value1 = dictionary[i];
            }
            stopwatch.Stop();
            Console.WriteLine(stopwatch.ElapsedMilliseconds);

            





        }
        public static Hashtable GetHashtable()
        {
            Hashtable hashtable = new Hashtable();
            hashtable.Add("名字", "小丽");
            hashtable.Add("年龄", 22);
            return hashtable;

        }

    }
}
